import { CodeCell, MarkdownCell, Notebook } from "./notebook/Notebook";

export class Course {
    constructor(public id : string, public name : string, public page : CoursePage[]) {}
}
export class CoursePage {
    constructor(public name : string, public markdown : string, public code : string, public preinput: string, public task : CourseTask[]) {}
}
export class CourseTask {
    constructor(public title : string, public description : string, public check : CourseCheck, public test : CourseCheck) {}
}
export class CourseCheck {
    constructor(public time : number, public before : string, public after : string) {}
}
export class CourseProgress {
    constructor(public id : string, public state : string = 'todo', public page : number = 0, public task : number = -1) {}
}
export class ExtendedCourse{
    constructor(public course: Course, public progress: CourseProgress){}
    public get url(): string[] {
        return ['course', this.course.id]
    }
    public get creatorUrl(): string[] {
        return ['creator', this.course.id]
    }
    public get maxLocation(): string{
        if (this.progress.state === 'todo') {
            return "未开始";
        }
        if (this.progress.state === 'finish') {
            return "已完成";
        }
        return `第${this.progress.page + 1}页任务${this.progress.task + 1}`;
    }
    public toNotebook(codeIndex: {[course:string]:{[page: number]: string}} = {}): Blob{
        const notebook = new Notebook();
        notebook.cells.push(new MarkdownCell('# ' + this.course.name))
        this.course.page.forEach((page, i)=>{
            const task = page.task.map(t=>'* ' + t.title).join('\n');
            const text = `${page.markdown}\n\n## 任务\n\n${task}`;
            notebook.cells.push(new MarkdownCell(text));
            if(this.course.id in codeIndex && i in codeIndex[this.course.id]){
                notebook.cells.push(new CodeCell(codeIndex[this.course.id][i]));
            }else{
                notebook.cells.push(new CodeCell(page.code));
            }
        });
        return notebook.toBlob();
    }
}